package com.example.wechat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.example.wechat.R;
import com.example.wechat.adapter.MomentAdapter;
import com.example.wechat.dialog.CommentDialog;
import com.example.wechat.model.Comment;
import com.example.wechat.model.Moment;
import com.example.wechat.model.UserManager;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MomentsActivity extends AppCompatActivity implements MomentAdapter.OnMomentClickListener {
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private FloatingActionButton fabPost;
    private MomentAdapter adapter;
    private List<Moment> moments;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moments);

        // 初始化视图
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("朋友圈");

        recyclerView = findViewById(R.id.recyclerView);
        swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout);
        fabPost = findViewById(R.id.fabPost);

        // 设置RecyclerView
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        moments = new ArrayList<>();
        adapter = new MomentAdapter(this, moments, this);
        recyclerView.setAdapter(adapter);

        // 设置下拉刷新
        swipeRefreshLayout.setOnRefreshListener(this::loadMoments);

        // 设置发布按钮
        fabPost.setOnClickListener(v -> {
            Intent intent = new Intent(this, PostMomentActivity.class);
            startActivity(intent);
        });

        // 加载数据
        loadMoments();
    }

    private void loadMoments() {
        // TODO: 从数据库加载朋友圈数据
        // 这里先使用测试数据
        moments.clear();
        moments.add(new Moment(
            "1",
            "张三",
            "https://example.com/avatar1.jpg",
            "今天天气真好！",
            new ArrayList<>(),
            System.currentTimeMillis(),
            "北京市朝阳区"
        ));
        adapter.notifyDataSetChanged();
        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_moments, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onMomentClick(Moment moment) {
        // 处理朋友圈点击事件
    }

    @Override
    public void onImageClick(Moment moment, int position) {
        // 处理图片点击事件
        Intent intent = new Intent(this, ImageViewerActivity.class);
        intent.putExtra("imageUrls", moment.getImages().toArray(new String[0]));
        intent.putExtra("position", position);
        startActivity(intent);
    }

    @Override
    public void onLikeClick(Moment moment) {
        // 处理点赞事件
        moment.setLiked(!moment.isLiked());
        if (moment.isLiked()) {
            moment.getLikes().add("我");
        } else {
            moment.getLikes().remove("我");
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onCommentClick(Moment moment) {
        // 处理评论事件
        CommentDialog dialog = new CommentDialog(this);
        dialog.setOnCommentSubmitListener(comment -> {
            // 处理评论提交
            Comment newComment = new Comment(
                UserManager.getInstance().getCurrentUser().getWechatId(),
                UserManager.getInstance().getCurrentUser().getNickname(),
                comment,
                new Date()
            );
            moment.getComments().add(newComment);
            adapter.notifyDataSetChanged();
        });
        dialog.show();
    }
} 